package DataBaseUtils

import (
	"github.com/elastic/go-elasticsearch/v7"
	"goGrpc/helloService/Utils"
	"log"
)

var EsConnection *elasticsearch.Client

//type esWorkerConnect struct {
//	Conn *elasticsearch.Client
//}

func NewEsConnect() {
	addresses := []string{configs.GetString("elasticSearch.address")}
	config := elasticsearch.Config{
		Addresses: addresses,
		Username:  configs.GetString("elasticSearch.userName"),
		Password:  configs.GetString("elasticSearch.passWord"),
		CloudID:   configs.GetString("elasticSearch.cloudId"),
		APIKey:    configs.GetString("elasticSearch.apiKey"),
	}
	log.Printf("Esconfig", config)
	// new client
	es, err := elasticsearch.NewClient(config)
	HelloServiceUtils.FailOnError(err, "Error creating the client")
	// Index creates or updates a document in an index
	res, err := es.Info()
	HelloServiceUtils.FailOnError(err, "Error getting response")
	log.Println(res.String(), "es链接结果")
	EsConnection = es
}
